#include "StdAfx.h"
#include "BasicWindowLessGif.h"
#include "BasicWindowLessHelp.h"

namespace WINDOWLESSUI
{
	CBasicWindowLessGif::CBasicWindowLessGif(CBasicWindowLessContainer* pContainer)
		:CBasicWindowLessElement(pContainer)
	{
		m_nFrameCount = 0;
		m_nCurFrame = 0;
		m_nResType = 0;
		m_pGif = NULL;
	}

	CBasicWindowLessGif::~CBasicWindowLessGif(void)
	{
		if (m_pGif)
			delete m_pGif;
	}

	void CBasicWindowLessGif::RenderElement(Graphics* gh)
	{
		RECT rc;
		this->GetRenderRect(&rc);
		if (m_pGif)
		{
			gh->DrawImage(m_pGif,rc.left,rc.top);
		}
	}

	void CBasicWindowLessGif::OnTimer(UINT nID)
	{
		if (nID == 100)
		{
			if ((++m_nCurFrame) >= m_nFrameCount)
			{
				m_nCurFrame = 0;
			}
			if (m_pGif && m_nResType == 0)
			{
				m_pGif->SelectActiveFrame(&FrameDimensionTime, m_nCurFrame);
			}
			this->InvalidateElement();
		}
	}

	void CBasicWindowLessGif::SetGif(Image* pGif)
	{
		m_nResType = 0;
		if (m_pGif)
			delete m_pGif;

		m_pGif = pGif;
		m_nFrameCount = 0;
		m_nCurFrame = 0;

		if (m_pGif && m_pGif->GetLastStatus() == Ok)
		{
			m_nCurFrame = 0;
			UINT uCount = m_pGif->GetFrameDimensionsCount();
			GUID* pDimensionIDs	= new GUID[uCount];
			m_pGif->GetFrameDimensionsList(pDimensionIDs, uCount);
			m_nFrameCount = m_pGif->GetFrameCount(&pDimensionIDs[0]);
			delete[]  pDimensionIDs;
		}
	}
	void CBasicWindowLessGif::SetPngGif(Image* pGif,int cx)
	{
		m_nResType = 1;
		if (m_pGif)
			delete m_pGif;
		m_pGif = pGif;

		m_nFrameCount = m_pGif->GetWidth()/cx;
		m_nCurFrame = 0;
	}
	void CBasicWindowLessGif::StartGif()
	{
		this->MySetTimer(100,100);
		this->ShowElement(true);
	}
	void CBasicWindowLessGif::StopGif()
	{
		this->MyKillTimer(100);
		this->ShowElement(false);
	}

	CWindowLessFrameGif::CWindowLessFrameGif(CBasicWindowLessContainer* pContainer)
		:CBasicWindowLessElement(pContainer)
	{
		m_nCur = -1;
	}

	CWindowLessFrameGif::~CWindowLessFrameGif(void)
	{
		ClearFrame();
	}

	void CWindowLessFrameGif::RenderElement(Graphics* gh)
	{
		RECT rc;
		this->GetRenderRect(&rc);
		if (m_nCur>=0 && (UINT)m_nCur < m_vFrameImg.size())
		{
			gh->DrawImage(m_vFrameImg[m_nCur],rc.left,rc.top);
		}
	}
	void CWindowLessFrameGif::OnTimer(UINT nID)
	{
		m_nCur++;
		if ((UINT)m_nCur >= m_vFrameImg.size())
		{
			m_nCur = 0;
		}
		this->InvalidateElement();
	}
	void CWindowLessFrameGif::StartGif()
	{
		m_nCur = 1;
		this->MySetTimer(100,100);
		this->ShowElement(true);
	}
	void CWindowLessFrameGif::StopGif()
	{
		this->MyKillTimer(100);
		this->ShowElement(false);
	}

	void CWindowLessFrameGif::ClearFrame()
	{
		m_nCur = 0;
		for(unsigned int i=0;i<m_vFrameImg.size();i++)
		{
			delete m_vFrameImg[i];
		}
		m_vFrameImg.clear();
	}

	void CWindowLessFrameGif::LoadLocalFrame()
	{
		ClearFrame();
		std::wstring strDir = CBasicWindowLessHelp::MakePathFromBase(L"res\\loading\\loading");
		std::wstring strFile;

		wchar_t buf[MAX_PATH];
		for(int i = 1;i<=10;i++)
		{
			wsprintf(buf,L"%d.png",i);
			strFile = strDir;
			strFile += buf;
			AddFrame(Image::FromFile(strFile.c_str()));
		}
	}
	void CWindowLessFrameGif::LoadLocalFrame2()
	{
		ClearFrame();
		std::wstring strDir = CBasicWindowLessHelp::MakePathFromBase(L"res\\loading1\\");
		std::wstring strFile;

		wchar_t buf[MAX_PATH];
		for(int i = 0;i<=9;i++)
		{
			wsprintf(buf,L"loading%d.png",i);
			strFile = strDir;
			strFile += buf;
			AddFrame(Image::FromFile(strFile.c_str()));
		}
	}
	void CWindowLessFrameGif::AddFrame(Image* pIm)
	{
		m_vFrameImg.push_back(pIm);

	}
}
